package longbottom.XLkprob;

import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class DualActivity extends ActionBarActivity implements
		OnItemSelectedListener, OnEditorActionListener {

	private GraphicalView mChart;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dual);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container1, new PlaceholderFragment()).commit();
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container2, new PlaceholderFragment()).commit();

		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mChart != null)
			mChart.repaint();
		else
			doUpdateGraph();
	}

	@Override
	public void onItemSelected(AdapterView<?> parentView, View v, int position,
			long id) {
		doUpdateGraph();
	}

	private void doUpdateGraph() {
		ViewGroup layout = (ViewGroup) findViewById(R.id.chart);
		ViewGroup controls1 = (ViewGroup) findViewById(R.id.container1);
		ViewGroup controls2 = (ViewGroup) findViewById(R.id.container2);

		if (mChart != null)
			layout.removeView(mChart); // delete any existing chart

		SimpleSituation a = SimpleSituation.parseControls(controls1);
		if (a == null)
			return;
		SimpleSituation b = SimpleSituation.parseControls(controls2);
		if (b == null)
			return;

		// create the graphs
		CategorySeries series1 = a.getCumulativeDistributionSeries(this);
		CategorySeries series2 = b.getCumulativeDistributionSeries(this);
		mChart = MyGraphFactory.makeDoubleChart(this, series1, series2);

		if (mChart != null)
			layout.addView(mChart);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);

			Context parentActivity = getActivity();

			EditText textShots = (EditText) rootView
					.findViewById(R.id.editText1);
			textShots.setText("2");

			Spinner spinnerToHit = (Spinner) rootView
					.findViewById(R.id.spinner1);
			Spinner spinnerToWound = (Spinner) rootView
					.findViewById(R.id.spinner2);
			Spinner spinnerToSave = (Spinner) rootView
					.findViewById(R.id.spinner3);
			Spinner spinnerRRHit = (Spinner) rootView
					.findViewById(R.id.spinner4);
			Spinner spinnerRRWound = (Spinner) rootView
					.findViewById(R.id.spinner5);
			Spinner spinnerRRSave = (Spinner) rootView
					.findViewById(R.id.spinner6);
			ArrayAdapter<CharSequence> adapter = ArrayAdapter
					.createFromResource(parentActivity, R.array.die_result,
							android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerToHit.setAdapter(adapter);
			spinnerToHit.setSelection(2);
			spinnerToWound.setAdapter(adapter);
			spinnerToWound.setSelection(3);
			spinnerToSave.setAdapter(adapter);
			spinnerToSave.setSelection(3);

			ArrayAdapter<CharSequence> adapter2 = ArrayAdapter
					.createFromResource(parentActivity, R.array.reroll_type,
							android.R.layout.simple_spinner_item);
			adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerRRHit.setAdapter(adapter2);
			spinnerRRHit.setSelection(0);
			spinnerRRWound.setAdapter(adapter2);
			spinnerRRWound.setSelection(0);
			spinnerRRSave.setAdapter(adapter2);
			spinnerRRSave.setSelection(0);

			textShots
					.setOnEditorActionListener((OnEditorActionListener) parentActivity);
			spinnerToHit
					.setOnItemSelectedListener((OnItemSelectedListener) parentActivity);
			spinnerToWound
					.setOnItemSelectedListener((OnItemSelectedListener) parentActivity);
			spinnerToSave
					.setOnItemSelectedListener((OnItemSelectedListener) parentActivity);
			spinnerRRHit
					.setOnItemSelectedListener((OnItemSelectedListener) parentActivity);
			spinnerRRWound
					.setOnItemSelectedListener((OnItemSelectedListener) parentActivity);
			spinnerRRSave
					.setOnItemSelectedListener((OnItemSelectedListener) parentActivity);

			return rootView;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
		doUpdateGraph();
		return false;
	}

}
